<?php
namespace app\manage\controller;
use app\manage\controller\Base;
use app\model\AdImgModel;
use app\model\AdvertisementModel;
use think\facade\Db;
use support\View;
use support\Request;

class AdvertisementController extends Base
{
    public function index(Request $request)
    {
    	$advertisement=new AdvertisementModel();
        $key=$request->get('key', '');
        View::assign('key',$key);
        $advertisement=$advertisement->where(function($query) use ($key){
                $query->whereor('key','like','%'.$key.'%');
            })->order('id desc')->paginate(['list_rows'=>10,'query'=>request()->all()]);
        View::assign('advertisement',$advertisement);
        View::assign('count1', $advertisement->total());
       	return view('advertisement/index');
    }
	public function ajax(Request $request)
    {
    	$data=$request->post();
		$advertisement=new AdvertisementModel();
		$ad_img=new AdImgModel();
		if($data['type']=='advertisement_del'){
			$id=$data['id'];
            $ad_img->where('ad_id',$id)->delete();
			$info=$advertisement->destroy($id);
			if($info){
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
		}
		if($data['type']=='advertisement_del_img'){
			if(AdImgModel::destroy($data['id'])){
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
		}
        return json(['code' => 0, 'message' => '失败']);
    }

	public function add_img(Request $request)
	{
		if(request()->method()=='POST'){
			$data=$request->post();
			if(!isset($data['isopen'])){
				$data['isopen']=0;
			}else{
				$data['isopen']=1;
			}
            $file = $request->file('img');
            if ($file && $file->isValid()  && $file->getSize() >0) {
                if(!in_array($file->getUploadExtension(), ['jpg','png','gif','webp','jpeg'])){return view_e('图片格式不正确');}
                if($file->getSize() > 5*1024*1024){return view_e('图片大小不能超过5M');}
                $path = '/advertisement/'.date("YmdHis").rand(10000,99999).session('uid', '0').'.'.$file->getUploadExtension();
                $file->move(public_path().$path);
                $data['img']=$path;
            }else{
                return view_e('图片必须填写');
            }
			$link=new AdImgModel();
			$link->data($data);
			$res=$link->save();
			if($res){
				return view_s('添加成功',url('advertisement/index',['st'=>1]));
			}else{
				return view_e('添加失败了');
			}
		}
        View::assign('ad_id', $request->input('id',0));
		return view('advertisement/add_img');
	}
	public function guan_img(Request $request)
	{
        $id=$request->input('id',0);
		$data=(new AdImgModel())->where('ad_id',$id)->order('id desc')->paginate(['list_rows'=>8,'query'=>request()->all()]);
        View::assign('data',$data);
        View::assign('count1', $data->total());
        View::assign('ad_id', $id);
	   	return view('advertisement/guan_img');
	}
	public function img_edit(Request $request)
	{
		if(request()->method()=='POST'){
			$data=$request->post();
			if(!isset($data['isopen'])){
				$data['isopen']=0;
			}else{
				$data['isopen']=1;
			}
            $file = $request->file('img');
            if ($file && $file->isValid()  && $file->getSize() >0) {
                if(!in_array($file->getUploadExtension(), ['jpg','png','gif','webp','jpeg'])){return view_e('图片格式不正确');}
                if($file->getSize() > 5*1024*1024){return view_e('图片大小不能超过5M');}
                $path = '/advertisement/'.date("YmdHis").rand(10000,99999).session('uid', '0').'.'.$file->getUploadExtension();
                $file->move(public_path().$path);
                $data['img']=$path;
            }
			$advertisement=new AdImgModel();
			$res=$advertisement->update($data,['id' => $data['id']]);
			if($res){
				return view_s('修改成功',url('advertisement/index',['st'=>1]));
			}else{
				return view_e('修改失败了');
			}
		}
		$id=$request->input('id',0);
		$data= Db::name('ad_img')->find($id);
		View::assign('data',$data);
	   return view('advertisement/img_edit');
	}

	public function add(Request $request)
    {
    	if(request()->method()=='POST'){
			$data=$request->post();
			$validate = new \app\manage\validate\AdvertisementValidate();
			if(!$validate->check($data)){
				return view_e($validate->getError());
			}	
			if(!isset($data['isopen'])){
				$data['isopen']=0;
			}else{
				$data['isopen']=1;
			}

            $file = $request->file('img');
            if ($file && $file->isValid()  && $file->getSize() >0) {
                if(!in_array($file->getUploadExtension(), ['jpg','png','gif','webp','jpeg'])){return view_e('图片格式不正确');}
                if($file->getSize() > 5*1024*1024){return view_e('图片大小不能超过5M');}
                $path = '/advertisement/'.date("YmdHis").rand(10000,99999).session('uid', '0').'.'.$file->getUploadExtension();
                $file->move(public_path().$path);
                $data['img']=$path;
            }
			$link=new AdvertisementModel();
			$link->data($data);
			$res=$link->save();
			if($res){
				return view_s('添加成功',url('advertisement/index',['st'=>1]));
			}else{
				return view_e('添加失败了');
			}
    	}
       return view('advertisement/add');
    }
	public function edit(Request $request)
    {
    	$advertisement = new AdvertisementModel();
    	if(request()->method()=='POST'){
    		$data=$request->post();
			$validate = new \app\manage\validate\AdvertisementValidate();
			if(!$validate->scene('edit')->check($data)){
				return view_e($validate->getError());
			}	
			if(!isset($data['isopen'])){
				$data['isopen']=0;
			}else{
				$data['isopen']=1;
			}
            $file = $request->file('img');
            if ($file && $file->isValid()  && $file->getSize() >0) {
                if(!in_array($file->getUploadExtension(), ['jpg','png','gif','webp','jpeg'])){return view_e('图片格式不正确');}
                if($file->getSize() > 5*1024*1024){return view_e('图片大小不能超过5M');}
                $path = '/advertisement/'.date("YmdHis").rand(10000,99999).session('uid', '0').'.'.$file->getUploadExtension();
                $file->move(public_path().$path);
                $data['img']=$path;
            }
			$res=$advertisement->update($data,['id' => $data['id']]);
			if($res){
				return view_s('修改成功',url('advertisement/index',['st'=>1]));
			}else{
				return view_e('修改失败了');
			}
    	}
		$id=$request->input('id',0);
		$data= $advertisement->find($id);
        View::assign('data',$data);
       return view('advertisement/edit');
    }
}
